<template>
    <div>
        <!-- 面包屑导肮 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card class="box-card">
            <el-button type="primary" @click="showAddDialog">添加分类</el-button>
            <tree-table :data="categoriesList" :columns="columns" :selection-type="false" :expand-type="false"
                index-text="#" :show-row-hover="false" class="cateTable" border show-index>
                <!-- 这个插件不支持最新的v-solt 语法 -->
                <template slot="isValidity" slot-scope="scope">
                    <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color :lightgreen;"></i>
                    <i class="el-icon-error" v-else></i>
                </template>
                <template slot="sort" slot-scope="scope">
                    <el-tag v-show="scope.row.cat_level===0">一级</el-tag>
                    <el-tag v-show="scope.row.cat_level===1" type="success">二级</el-tag>
                    <el-tag v-show="scope.row.cat_level===2" type="warning">三级</el-tag>
                </template>
                <template slot="operation" slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" size="small"
                        @click="showUpdateDialog(scope.row.cat_id)">编辑</el-button>
                    <el-button type="danger" icon="el-icon-delete" size="small" @click="openDeleteBox(scope.row.cat_id)">删除
                    </el-button>

                </template>
            </tree-table>
            <el-pagination @size-change="cateSizeChange" @current-change="cateCurrentChange"
                :current-page="queryInfo.pagenum" :page-sizes="[5,10, 15]" :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-card>
        <!-- 添加修改对话框 -->
        <el-dialog :title="this.addUpdateFormOper === 'add'?'添加分类':'修改分类'" :visible.sync="isShowDialog" width="30%">
            <span>
                <!-- 添加用户表单 -->
                <el-form label-width="85px" class="add_form" :model="addUpdateForm" :rules="addRules"
                    ref="addUpdateForm">
                    <!-- 分类名 -->
                    <el-form-item label="分类名称" prop="cat_name">
                        <el-input v-model="addUpdateForm.cat_name"></el-input>
                    </el-form-item>
                    <!-- 父级分类 -->
                    <el-form-item label="父级分类">
                        <el-cascader expand-trigger="hover" :options="parentCateList" :props="cateCascaderProps"
                            v-model="parentSelectedKeys" @change="parentCateChange" clearable change-on-select>
                        </el-cascader>
                    </el-form-item>

                </el-form>
            </span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogCancle">取 消</el-button>
                <el-button type="primary" @click="dialogOK">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {
    reqCategory,
    reqAddCategory,
    reqCategoryById,
    reqUpdateCate,
    reqDeleteCateById
} from '@/api'
export default {
    created() {
        //发送请求拿到用户列表
        this.getCateList(), this.getParentCateList()
    },

    data() {
        return {
            categoriesList: [],
            queryInfo: {
                type: 3,
                pagenum: 1,
                pagesize: 5
            },
            total: 0,
            columns: [
                {
                    label: '分类名称',
                    prop: 'cat_name'
                },
                {
                    label: '是否有效',
                    type: 'template',
                    template: 'isValidity'
                },
                {
                    label: '排序',
                    type: 'template',
                    template: 'sort'
                },
                {
                    label: '操作',
                    type: 'template',
                    template: 'operation'
                }
            ],
            isShowDialog: false,
            addUpdateForm: {
                cat_name: '',
                cat_pid: 0,
                cat_level: 0
            },
            addRules: {
                cat_name: [
                    {
                        required: true,
                        message: '请输入分类名称',
                        trigger: 'blur'
                    }
                ]
            },
            addUpdateFormOper: 'add',
            parentCateList: [],
            parentSelectedKeys: [],
            cateCascaderProps: {
                value: 'cat_id',
                label: 'cat_name',
                children: 'children'
            },
            currentCateId: ''
        }
    },
    methods: {
        async getCateList() {
            const {
                data,
                meta: { msg, status }
            } = await reqCategory(this.queryInfo)
            if (200 !== status) return this.$message.error(msg)

            this.total = data.total
            this.categoriesList = data.result
        },
        cateSizeChange(val) {
            this.queryInfo.pagesize = val
            this.getCateList()
        },
        cateCurrentChange(val) {
            this.queryInfo.pagenum = val
            this.getCateList()
        },
        showAddDialog() {
            //打开后重新渲染dom 再选然后执行清空就不会报错了
            this.addUpdateFormOper = 'add'

            this.isShowDialog = true
        },
        async getParentCateList() {
            const {
                data,
                meta: { msg, status }
            } = await reqCategory({ type: 2 })
            if (200 !== status) return this.$message.error(msg)

            this.parentCateList = data
        },
        showUpdateDialog(id) {
            this.addUpdateFormOper = 'edit'
            this.reqCategoryById(id)
            this.currentCateId = id
            this.isShowDialog = true
        },
        async reqCategoryById(id) {
            const {
                data,
                meta: { msg, status }
            } = await reqCategoryById(id)

            if (200 !== status) return this.$message.error(msg)
            this.parentSelectedKeys.push(data.cat_id, data.cat_pid)
            this.addUpdateForm = data
        },
        dialogCancle() {
            this.isShowDialog = false
            if (this.addUpdateFormOper !== 'add') return
            this.addUpdateForm.cat_name = ''
            this.parentSelectedKeys = []
        },
        dialogOK() {
            this.$refs.addUpdateForm.validate(async (valid) => {
                // 验证有误
                if (!valid) return

                if (this.addUpdateFormOper === 'add') {
                    // 发送添加用户请求
                    // console.log(this.addUpdateForm)
                    const {
                        data,
                        meta: { msg, status }
                    } = await reqAddCategory(this.addUpdateForm)
                    if (201 !== status) return this.$message.error(msg)
                    this.$message.success(msg)
                    this.getCateList()
                    this.addUpdateForm.cat_name = ''
                    this.parentSelectedKeys = []
                    this.isShowDialog = false
                } else {
                    //发送修改用户请求
                    const {
                        addUpdateForm: { cat_name },
                        currentCateId
                    } = this
                    const {
                        data,
                        meta: { msg, status }
                    } = await reqUpdateCate(currentCateId, cat_name)
                    if (200 !== status) return this.$message.error(msg)
                    this.$message.success(msg)
                    this.getCateList()
                    this.isShowDialog = false
                }
            })
        },
        parentCateChange(val) {
            this.parentSelectedKeys = val
            if (this.parentSelectedKeys.length > 0) {
                //父级分类id
                ;(this.addUpdateForm.cat_pid = this.parentSelectedKeys[
                    this.parentSelectedKeys.length - 1
                ]),
                    //分类等级
                    (this.addUpdateForm.cat_level = this.parentSelectedKeys.length)
            } else {
                //清空时触发
                this.addUpdateForm.cat_pid = 0
                this.addUpdateForm.cat_level = 0
            }
        },
        async deleteCate(id) {
            const {
                meta: { msg, status }
            } = await reqDeleteCateById(id)
            if (200 !== status) return this.$message.error(msg)
            this.$message.success(msg)
            this.queryInfo.pagenum = 1
            this.getCateList()
        },
        openDeleteBox(id) {
            this.$confirm('是否删除该分类, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                     
                    this.deleteCate(id)
                })
                .catch(() => {
               
                })
        }
    }
}
</script>
<style lang="less" scoped>
.cateTable {
    text-align: center;
}
</style>